﻿using Dapper;
using System;
using BCEntity.OA.WorkflowTemplate.WorkflowTemplateStepFormConfig;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using BCCommon;

namespace BCData.OA.WorkflowTemplate.WorkflowTemplateStepFormConfig
{
	/// <summary>
	/// 工作流模板步骤表单配置
	/// </summary>
	public class WorkflowTemplateStepFormConfigData : IWorkflowTemplateStepFormConfigData
	{
		private readonly IDatabaseContext databaseContext;
		private const string QUERY_EXISTS = @"SELECT 1 FROM oa_workflowtemplatestepformconfig";
		private const string QUERY = @"SELECT WorkflowTemplateStepFormConfigId,EditAdminName,UpdateTime,FormCategoryCode,FormCategoryName,FormConfigTitle,FormConfigCode,
											IsSpecificForm,SpecificFormName,CreateAdminId,CreateAdminName,CreateTime,EditAdminId 
										FROM oa_workflowtemplatestepformconfig";
		public WorkflowTemplateStepFormConfigData(IDatabaseContext databaseContext)
		{
			this.databaseContext = databaseContext;
		}
		#region 检查工作流模板步骤表单配置是否存在
		public async Task<bool> Exists(long workflowTemplateStepFormConfigId)
		{
			string sql = string.Format(@"{0} WHERE WorkflowTemplateStepFormConfigId=?WorkflowTemplateStepFormConfigId", QUERY_EXISTS);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?WorkflowTemplateStepFormConfigId", workflowTemplateStepFormConfigId, DbType.Int64);
			return await this.databaseContext.Master.ExecuteScalarAsync(sql, parameters) != null;
		}
		#endregion

		#region 添加工作流模板步骤表单配置
		public async Task<WorkflowTemplateStepFormConfigEntity> Add(WorkflowTemplateStepFormConfigEntity entity)
		{
			string sql = SqlCommands.GetInsertSql("oa_workflowtemplatestepformconfig", new string[] { "FormCategoryCode", "FormCategoryName", "FormConfigTitle", "FormConfigCode", "IsSpecificForm", "SpecificFormName", "CreateAdminId", "CreateAdminName" });
			sql = string.Format("{0} {1} WHERE WorkflowTemplateStepFormConfigId=LAST_INSERT_ID();", sql, QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?FormCategoryCode", entity.FormCategoryCode?.Trim(), DbType.AnsiString, size: 50);
			parameters.Add("?FormCategoryName", entity.FormCategoryName?.Trim(), DbType.AnsiString, size: 30);
			parameters.Add("?FormConfigTitle", entity.FormConfigTitle?.Trim(), DbType.AnsiString, size: 100);
			parameters.Add("?FormConfigCode", entity.FormConfigCode?.Trim(), DbType.AnsiString, size: 100);
			parameters.Add("?CreateAdminId", entity.CreateAdminId, DbType.Int64, size: 20);
			parameters.Add("?SpecificFormName", entity.SpecificFormName?.Trim(), DbType.AnsiString, size: 100);
			parameters.Add("?IsSpecificForm", entity.IsSpecificForm, DbType.Boolean);
			parameters.Add("?CreateAdminName", entity.CreateAdminName?.Trim(), DbType.AnsiString, size: 20);
			return await this.databaseContext.Master.QueryFirstOrDefaultAsync<WorkflowTemplateStepFormConfigEntity>(sql, parameters, databaseContext.Transaction);

		}
		#endregion

		#region 修改工作流模板步骤表单配置
		public async Task<WorkflowTemplateStepFormConfigEntity> Update(WorkflowTemplateStepFormConfigEntity entity)
		{
			string sql = SqlCommands.GetUpdateSql("oa_workflowtemplatestepformconfig", new string[] { "EditAdminName", "FormCategoryCode", "FormCategoryName", "FormConfigTitle", "FormConfigCode", "IsSpecificForm", "SpecificFormName", "EditAdminId" }, new string[] { "WorkflowTemplateStepFormConfigId" });
			sql = string.Format("{0} {1} WHERE WorkflowTemplateStepFormConfigId=?WorkflowTemplateStepFormConfigId;", sql, QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?WorkflowTemplateStepFormConfigId", entity.WorkflowTemplateStepFormConfigId, DbType.Int64, size: 20);
			parameters.Add("?EditAdminName", entity.EditAdminName?.Trim(), DbType.AnsiString, size: 20);
			parameters.Add("?FormCategoryCode", entity.FormCategoryCode?.Trim(), DbType.AnsiString, size: 50);
			parameters.Add("?FormCategoryName", entity.FormCategoryName?.Trim(), DbType.AnsiString, size: 30);
			parameters.Add("?FormConfigTitle", entity.FormConfigTitle?.Trim(), DbType.AnsiString, size: 100);
			parameters.Add("?FormConfigCode", entity.FormConfigCode?.Trim(), DbType.AnsiString, size: 100);
			parameters.Add("?SpecificFormName", entity.SpecificFormName?.Trim(), DbType.AnsiString, size: 100);
			parameters.Add("?IsSpecificForm", entity.IsSpecificForm, DbType.Boolean);
			parameters.Add("?EditAdminId", entity.EditAdminId, DbType.Int64, size: 20);
			return await this.databaseContext.Master.QueryFirstOrDefaultAsync<WorkflowTemplateStepFormConfigEntity>(sql, parameters, databaseContext.Transaction);
		}
		#endregion

		#region 删除工作流模板步骤表单配置
		public async Task<bool> Delete(long workflowTemplateStepFormConfigId)
		{
			string sql = @"DELETE FROM oa_workflowtemplatestepformconfig WHERE WorkflowTemplateStepFormConfigId=?WorkflowTemplateStepFormConfigId";
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?WorkflowTemplateStepFormConfigId", workflowTemplateStepFormConfigId, DbType.Int64);
			return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
		}
		#endregion

		#region 获取工作流模板步骤表单配置
		public async Task<WorkflowTemplateStepFormConfigEntity> GetEntity(long workflowTemplateStepFormConfigId)
		{
			string sql = string.Format(@"{0} WHERE WorkflowTemplateStepFormConfigId=?WorkflowTemplateStepFormConfigId", QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?WorkflowTemplateStepFormConfigId", workflowTemplateStepFormConfigId, DbType.Int64);
			return await this.databaseContext.Slave.QueryFirstOrDefaultAsync<WorkflowTemplateStepFormConfigEntity>(sql, parameters);
		}
		public async Task<WorkflowTemplateStepFormConfigEntity> GetEntity(string formConfigCode)
		{
			string sql = string.Format(@"{0} WHERE FormConfigCode=?FormConfigCode", QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?FormConfigCode", formConfigCode, DbType.AnsiString, size: 100);
			return await this.databaseContext.Slave.QueryFirstOrDefaultAsync<WorkflowTemplateStepFormConfigEntity>(sql, parameters);
		}
		#endregion

		#region 获取工作流模板步骤表单配置列表
		public async Task<IEnumerable<WorkflowTemplateStepFormConfigEntity>> GetList(string formCategoryCode)
		{
			string sql = string.Format(@"{0} WHERE FormCategoryCode=?FormCategoryCode", QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?FormCategoryCode", formCategoryCode, DbType.AnsiString);
			return await this.databaseContext.Slave.QueryAsync<WorkflowTemplateStepFormConfigEntity>(sql, parameters);
		}
		#endregion

		#region 获取工作流模板步骤表单配置分页
		public async Task<(IEnumerable<WorkflowTemplateStepFormConfigEntity>, int)> Query(string formCategoryCode, string formConfigTitle, DateTime? start, DateTime? end, string sortName, bool? ascending, int pageIndex, int pageSize)
		{
			List<string> wheres = new List<string>();
			DynamicParameters parameters = new DynamicParameters();
            if (!string.IsNullOrWhiteSpace(formCategoryCode))
            {
				wheres.Add("FormCategoryCode=?FormCategoryCode");
				parameters.Add("?FormCategoryCode", formCategoryCode, DbType.AnsiString);
			}
			if (!string.IsNullOrWhiteSpace(formConfigTitle))
			{
				wheres.Add("FormConfigTitle LIKE CONCAT('%',CONCAT(?FormConfigTitle,'%'))");
				parameters.Add("?FormConfigTitle", formConfigTitle, DbType.AnsiString);
			}
			if (start.HasValue && !end.HasValue)
			{
				wheres.Add("CreateTime>=?Start");
				parameters.Add("?Start", start.Value, DbType.DateTime);
			}
			else if (!start.HasValue && end.HasValue)
			{
				wheres.Add("CreateTime<=?End");
				parameters.Add("?End", end.Value, DbType.DateTime);
			}
			else if (start.HasValue && end.HasValue)
			{
				wheres.Add("CreateTime>=?Start AND CreateTime<=?End");
				parameters.Add("?Start", start.Value, DbType.DateTime);
				parameters.Add("?End", end.Value, DbType.DateTime);
			}
			string sql = string.Format(@"{0} {1} ORDER BY {4} {5} LIMIT {2},{3}; SELECT COUNT(1) FROM oa_workflowtemplatestepformconfig {1}",
				  QUERY,
				  wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "",
				  (pageIndex == 0 ? 0 : pageIndex - 1) * pageSize,
				  pageSize,
				  string.IsNullOrWhiteSpace(sortName) ? "CreateTime" : sortName,
				  ascending.HasValue && ascending.Value ? "ASC" : "DESC");
			return await this.databaseContext.Slave.QueryMultipleAsync<WorkflowTemplateStepFormConfigEntity, int>(sql, parameters);
		}
        #endregion
    }
}



